<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category PermissionService.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/7  18:01
 * +--------------------------------------------------------+
 */

namespace App\Services;

use App\Repositories\PermissionRepository;
use App\Util\Arr;

class PermissionService extends BaseService
{
    protected $permissionRepository;

    public function __construct(PermissionRepository $permissionRepository)
    {
        $this->permissionRepository = $permissionRepository;
    }

    /**
     * @param $params
     * @return array
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/8  17:47
     */
    public function getList($params)
    {
        $data = $this->permissionRepository->getList($params);

        return $this->initPaginateData($data);
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/8  17:53
     */
    public function getInfo($id)
    {
        return $this->permissionRepository->getInfo($id);
    }

    /**
     * @param $params
     * @return array
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/8  17:47
     */
    public function getTree($params)
    {
        $data = $this->permissionRepository->getTree($params);

        return Arr::getTree($data->toArray());
    }

    /**
     * @param $param
     * @return false|int
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  11:39
     */
    public function drop($param)
    {
        $update = [];

        switch ($param['type']){
            case 'before':
                $dropPermission = $this->permissionRepository->getInfo($param['drop']);
                $update = ['sort' => $dropPermission->sort + 1, 'pid' => $dropPermission->pid];
                break;
            case 'inner':
                $update = ['pid' => $param['drop']];
                break;
            case 'after':
                $dropPermission = $this->permissionRepository->getInfo($param['drop']);
                $sort = $dropPermission->sort;
                $update = ['sort' => ($sort - 1) < 1 ? 1 : $sort - 1, 'pid' => $dropPermission->pid];
                break;
            default:
        }

        if (count($update) > 0) {
            return $this->permissionRepository->update($param['dragging'], $update);
        }

        return false;
    }

    /**
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  11:40
     */
    public function create($params)
    {
        return $res = $this->permissionRepository->create($params);
    }

    /**
     * @param $permission
     * @param $params
     * @return mixed
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  11:49
     */
    public function update($permission, $params)
    {
        return $res = $this->permissionRepository->updateByModel($permission, $params);
    }

    /**
     * @param $permission
     * @return mixed
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  11:50
     */
    public function delete($permission)
    {
        return $res = $this->permissionRepository->delete($permission);
    }



}
